package com.tiantian.system.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.tiantian.api.RemoteUserService;
import com.tiantian.api.domain.vo.RemoteUserVo;
import com.tiantian.common.core.constant.SystemConfigConstants;
import com.tiantian.common.core.constant.UserConstants;
import com.tiantian.common.excel.utils.ExcelUtil;
import com.tiantian.common.log.annotation.Log;
import com.tiantian.common.log.enums.BusinessType;
import com.tiantian.common.log.enums.OperatorType;
import com.tiantian.common.redis.utils.RedisUtils;
import com.tiantian.system.constants.EnrollResultConstants;
import com.tiantian.system.domain.excel.NewcomerForExcel;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 数据导出接口
 *
 * @author TianTian
 */
@RestController
@RequestMapping("/system/export")
@RequiredArgsConstructor
public class ExportController {

    @DubboReference
    private final RemoteUserService remoteUserService;


    @GetMapping("/{type}")
    @Log(businessType = BusinessType.FILE_EXPORT, isSaveResponseData = false, isSaveRequestData = false, operatorType = OperatorType.ADMIN)
    public void exportNewcomerExcel(HttpServletResponse response, @PathVariable String type) throws IOException {
        RemoteUserVo remoteUserVo = new RemoteUserVo();
        List<List<String>> head = new ArrayList<>();
        List<String> title = new ArrayList<>();
        if ("newcomer".equals(type)) {
            title.add("新人信息");
            remoteUserVo = null;
        } else if ("scoreThrough".equals(type)) {
            title.add("评分通过名单");
            HashMap<String, Integer> scoreLineMap = RedisUtils.getCacheObject(SystemConfigConstants.SYSTEM_CONFIG_INTERVIEW_SCORE_LINE);
            Integer scoreLine = scoreLineMap.get("scoreLine");
            remoteUserVo.setTotalScore(scoreLine);
        } else if ("enrollThrough".equals(type)) {
            title.add("录取通过名单");
            remoteUserVo.setEnrollStatus(EnrollResultConstants.ENROLL_PASS);
        } else {
            return;
        }
        head.add(title);

        // 查询数据
        List<RemoteUserVo> newcomerInfoList = remoteUserService.getNewcomerList(remoteUserVo);
        // 属性拷贝
        List<NewcomerForExcel> newcomerForExcels = BeanUtil.copyToList(newcomerInfoList, NewcomerForExcel.class);
        for (NewcomerForExcel newcomerForExcel : newcomerForExcels) {
            if (UserConstants.female.equals(newcomerForExcel.getSex())) {
                newcomerForExcel.setSex("女");
            } else if (UserConstants.male.equals(newcomerForExcel.getSex())) {
                newcomerForExcel.setSex("男");
            } else {
                newcomerForExcel.setSex("未知");
            }
        }
        response.setContentType("application/vnd.ms-excel;chartset=utf-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(System.currentTimeMillis() + "导出", StandardCharsets.UTF_8);
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), NewcomerForExcel.class)
                .head(head)
                .head(NewcomerForExcel.class)
                .autoCloseStream(true)
                .sheet("基本信息")
                .doWrite(newcomerForExcels);
    }

    // 导出判题结果
    @GetMapping("/result/judge")
    @Log(businessType = BusinessType.FILE_EXPORT, isSaveResponseData = false, isSaveRequestData = false, operatorType = OperatorType.ADMIN)
    public void exportJudgeResultExcel(HttpServletResponse response) throws IOException {
        // List<SysJudgeVO> sysJudgeVOList = sysJudgeService.selectJudgeList(null);
        // 属性拷贝
        /*List<JudgeResultForExcel> judgeResultForExcels = BeanUtil.copyToList(sysJudgeVOList, JudgeResultForExcel.class);
        response.setContentType("application/vnd.ms-excel;chartset=utf-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(System.currentTimeMillis() + "导出", StandardCharsets.UTF_8);
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), NewcomerForExcel.class)
                .head(JudgeResultForExcel.class)
                .autoCloseStream(true)
                .sheet("基本信息")
                .doWrite(judgeResultForExcels);*/
    }


}
